package labox.innovation.gameserver.network.clientpackets;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import labox.innovation.config.Config;
import labox.innovation.gameserver.datatables.ItemTable;
import labox.innovation.gameserver.datatables.SkillTable;
import labox.innovation.gameserver.datatables.SkillTreeTable;
import labox.innovation.gameserver.model.FuzePracticeSkillLearn;
import labox.innovation.gameserver.model.L2Skill;
import labox.innovation.gameserver.model.L2SkillLearn;
import labox.innovation.gameserver.model.actor.instance.FuzePcInstance;
import labox.innovation.gameserver.network.serverpackets.ClientScriptHandle;
import labox.innovation.gameserver.templates.item.L2Item;
import labox.innovation.gameserver.templates.item.FuzeEconomicManager.OPSTATE;

/**
 * 
 * 技能学习 或 提升协议
 * 
 * @author zjj
 * 
 */
public final class RequestAquireskill extends L2GameClientPacket {
	private static final String _C__2f_REQUESTAQUIRESKILL = "[C] 0x2f RequestAquireSkill";

	private static final Logger _log = LoggerFactory.getLogger(RequestAquireskill.class.getName());

	private static final int SKILLTYPE_CLASSID = 0;

	private static final int SKILLTYPE_PLE = 1;

	private int _id; //技能id

	private int _skillType;//技能类型   0:职业技能  1:仙魔位技能

	@Override
	protected void readImpl() {
		_id = readInt();
		_skillType = readByte();
	}

	@Override
	protected void runImpl() {

		if (_log.isDebugEnabled()) {
			_log.debug("技能id:{}", _id);
			_log.debug("技能类型:{}", _skillType);
		}

		final FuzePcInstance player = getClient().getActiveChar();
		if (player == null)
			return;

		//当前已学的技能等级
		int level = player.getSkillLevel(_id);
		if (level == -1) {
			level = 0;
		}

		StringBuilder skillStr = new StringBuilder();

		final L2Skill skill = SkillTable.getInstance().getInfo(_id, level + 1);
		if (skill == null) {
			return;
		}

		if (level + 1 > skill.getSkillLearnLvl()) {
			//TODO:
			//已学到最高级
			return;
		}

		L2SkillLearn skLeran = null;
		switch (_skillType) {
		case SKILLTYPE_CLASSID:
			skLeran = SkillTreeTable.getInstance().getLearnSkillCon(player.getClassId().getId(), _id, level + 1);
			if (skLeran == null) {
				return;
				//					player.addSkill( skill , true );
			} else {
				if (skLeran.canLearn(player)) { //条件满足
					player.addSkill(skill, true);
				} else {

					return;
				}
			}
			skillStr.append("updata(").append(_skillType).append(',').append(_id).append(',').append(level + 1).append(')');
			destoryItems(player, skLeran);
			break;
		case SKILLTYPE_PLE:
			//仙魔位境界 为字符串
			//			skLeran = SkillTreeTable.getInstance().getPracticeSkill(player.getClassId().getId(), player.getPracticeId().getId(), _id);
			//				skLeran = SkillTreeTable.getInstance().getPracticeSkill( player.getClassId().getId(), player.getPracticeId().getId(), _id);
			skLeran = SkillTreeTable.getInstance().getSkillLearn(_id, level + 1);
			if (skLeran == null) {
				_log.error("没有技能学习条件,仙魔位id:" + player.getPracticeId().getId(), Config.SERVER_ID);
				return;
				//					player.addSkill( skill , true );
			} else {
				if (skLeran instanceof FuzePracticeSkillLearn && ((FuzePracticeSkillLearn) skLeran).canLearn(player)) { //条件满足
					player.addSkill(skill, true);
				} else {

					return;
				}
			}
			skillStr.append("updata(").append(_skillType).append(',').append(_id).append(',').append(level + 1).append(')');
			destoryItems(player, skLeran);
			break;
		}

		if (skillStr.length() > 0) {
			//通知客户端更新技能树
			if (_log.isDebugEnabled())
				_log.debug("通知客户端{} 技能升级成功", player);
			player.sendPacket(new ClientScriptHandle("skill", skillStr.toString()));
			skillStr.setLength(0);
			_log.info(skillStr.append("学习技能:").append(skill.getName()).append('(').append(skill.getId()).append(')').append("角色 ").append(player.getName()).append('(').append(player.getObjectId()).append(')').toString(), player.getObjectId(), player.getName());
		}

	}

	private void destoryItems(FuzePcInstance player, L2SkillLearn skLeran) {
		if (skLeran == null) {
			return;
		}

		if (skLeran.getExp_consume() > 0) {
			player.addExp(-skLeran.getExp_consume());
		}

		//消耗技能物品
		if ((skLeran.getItems() != null && skLeran.getItems().length > 0) && (skLeran.getItemNums() != null && skLeran.getItemNums().length > 0) && (skLeran.getItems().length == skLeran.getItemNums().length)) {
			int i = 0;
			for (int id : skLeran.getItems()) {
				
				L2Item it = ItemTable.getInstance().getTemplate( id );
				if( it != null && it.isMoney() ){
					player.reduceMoneySilver(OPSTATE.LEARNSKILL.getValue(), skLeran.getItemNums()[i++], null, true);
				}else{
					player.destroyItemByItemId("技能消耗", id, skLeran.getItemNums()[i++], null, true);
				}
				
			}// end for
			
		}// end 

	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see labox.innovation.gameserver.clientpackets.ClientBasePacket#getType()
	 */
	@Override
	public String getType() {
		return _C__2f_REQUESTAQUIRESKILL;
	}
}
